package opertation;


import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * 新增书籍
 */
public class AddOpertation implements IOPertation{

    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("请输入类别");
        String type = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        int sizeof = bookList.getBooksSizeof();
        int i = 0;
        for (; i < sizeof; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("已有该图书 添加失败 ！！！");
                return ;
            }
        }
        if(i == bookList.getBooks().length) {
            System.out.println("图书已满 添加失败 ！！！");
            return ;
        }
        Book book = new Book(name,author,price,type);
        bookList.setBook(sizeof,book);
        bookList.setBooksSizeof(sizeof + 1);
        System.out.println("添加成功");
    }
}
